//
//  BoundaryMangerViewController.swift
//  Elearning
//
//  Created by rimi on 16/1/25.
//  Copyright © 2016年 rimi. All rights reserved.
//

import UIKit

class BoundaryMangerViewController: UIViewController ,UITextFieldDelegate{
    
    var viewControllers :[UIViewController] = []
    
    var buttonImages : [UIImage] = []
    
    var buttonSelectedImages : [UIImage]?
    
    
    
  
    

    
    
    
    let buttonTag = 200
    
    
    let redLine : UIView = {
        let newViw = UIView()
        newViw.backgroundColor = UIColor.redColor()
        
        return newViw
    }()
    
    lazy var leftImageView:UIImageView = {
        let newView = UIImageView()
        
        newView.image = UIImage.init(named: "logo.png")
       
        
        
        return newView
        
    }()
    
    lazy var mask :UIView = {
        
        let newView = UIView()
        newView.backgroundColor  = UIColor ( red: 0.4, green: 0.4, blue: 0.4, alpha: 0.3 )
        newView.frame = CGRect.init(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        newView.alpha = 0;
        let MaskViewGesture = UITapGestureRecognizer.init(target: self, action: "respondsToTapGesture:")
        newView.addGestureRecognizer(MaskViewGesture)
        return newView

        
    }()
    
    lazy var topBarView:UIView = {
        
        let newView = UIView()
        newView.frame = CGRect.init(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height*0.097)
        newView.backgroundColor =  UIColor ( red: 0.8706, green: 0.8784, blue: 0.8784, alpha: 1.0 )
        
        
        return newView
        
    }()
    
    lazy var tabBarView:UIView = {
        
        let newView = UIView()
        newView.frame = CGRect.init(x: 0, y: self.view.bounds.height - self.view.bounds.height*0.097, width: self.view.bounds.width, height: self.view.bounds.height*0.097)
        newView.backgroundColor =  UIColor ( red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0 )
        
        
        return newView
        
    }()
    
    lazy var searchTextField:UITextField = {
        
        let newView = UITextField()

        newView.placeholder = "请输入课程名"
        newView.textAlignment = .Center
        newView.backgroundColor = UIColor.blueColor()
        
        
        return newView
        
    }()
    
    
    
    lazy var searchView:UIView = {
       let newView = UIView()
        newView.backgroundColor = UIColor.whiteColor()
        let hintLabel = UILabel()
        newView.addSubview(hintLabel)
        
        hintLabel.autoConstrainAttribute(.Horizontal, toAttribute: .Horizontal, ofView: newView)
        hintLabel.autoConstrainAttribute(.Vertical, toAttribute: .Vertical, ofView: newView)
        hintLabel.text  = "输入名称后点击'搜索'键进行搜索"
        
        hintLabel.autoSetDimensionsToSize(CGSizeMake(250, 30))
        hintLabel.textAlignment = .Center
        newView.bounds  = CGRect.init(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height*0.3)
        return newView
        
    }()
    
    var selecedIndex = -1   {
        willSet{
            if selecedIndex >= 0 {
                viewControllers[selecedIndex].view.removeFromSuperview()
                if let button =  self.view.viewWithTag(selecedIndex + buttonTag) as?UIButton{
                    button.selected = false
                }
            }
            
            
            
            
//            asefwefw÷
    
        }
        didSet{
            self.view.insertSubview(viewControllers[selecedIndex].view, atIndex: 0)
            if let button  = self.view.viewWithTag(selecedIndex + buttonTag) as?UIButton{
                button.selected  = true
                var center = button.center
                center.y  = self.redLine.bounds.height/2
                
                UIView.animateWithDuration(0.25 , animations: { () -> Void in
                 self.redLine.center = center
                    
                })
            }
        }
    }
    init(viewControllers:[UIViewController] ,images:[UIImage],selectedImage:[UIImage]?){
        self.viewControllers = viewControllers
        self.buttonImages = images
        if let _ = selectedImage{
            buttonSelectedImages = selectedImage
        }
        super.init(nibName: nil, bundle: nil)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("sdsaadfrggggggg")
        self.view .addSubview(self.topBarView)
        self.view .addSubview(self.tabBarView)
        self.topBarView.addSubview(self.searchTextField)
        self.topBarView.addSubview(self.leftImageView)
        
        self.leftImageView.autoConstrainAttribute(.Horizontal, toAttribute: .Horizontal, ofView: self.topBarView)
   
        self.leftImageView.autoConstrainAttribute(.Vertical, toAttribute: .Vertical, ofView: self.topBarView, withMultiplier: 0.2)
        self.leftImageView.autoSetDimensionsToSize(CGSizeMake(self.view.bounds.width*0.2, self.view.bounds.height*0.05))
        
        
        
        self.searchTextField.autoConstrainAttribute(.Horizontal, toAttribute: .Horizontal, ofView: self.topBarView)
        self.searchTextField.autoConstrainAttribute(.Vertical, toAttribute: .Vertical, ofView: self.topBarView, withMultiplier: 1.78)
        self.searchTextField.autoSetDimensionsToSize(CGSizeMake(self.view.bounds.width*0.2, self.view.bounds.height*0.05))
       initController()
     
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if selecedIndex < 0 {
            selecedIndex = 0;
        }
    }
    private func initController(){
        
        
        for(index, value) in buttonImages.enumerate(){
            let button = UIButton.init(type: .Custom)
            button.setImage(value, forState: .Normal)
            
            button.tag = buttonTag + index
            button.addTarget(self, action: "tapedButton:", forControlEvents: .TouchUpInside)
            if let selectedImage  = buttonSelectedImages?[index]{
                button.setImage(selectedImage, forState: .Selected)
            }
            
            button.imageView?.contentMode = .ScaleAspectFit
            self.tabBarView.addSubview(button)
            
            button.autoMatchDimension(.Width, toDimension: .Width, ofView: self.tabBarView, withMultiplier: 1.0/CGFloat(viewControllers.count))
            button.autoMatchDimension(.Height, toDimension: .Height, ofView: self.tabBarView, withMultiplier: 0.9)
            
            button.autoConstrainAttribute(.Horizontal, toAttribute: .Horizontal, ofView: self.tabBarView)
            if index == 0{
                button.autoPinEdgeToSuperviewEdge(.Left)
            }else{
                if let lastButton = self.view.viewWithTag(buttonTag + index - 1)as?UIButton{
                    button.autoPinEdge(.Left, toEdge: .Right, ofView: lastButton)
                    
                }
            }

        }
        for VC in viewControllers{
            self.addChildViewController(VC)
            VC.didMoveToParentViewController(self)
        }
        let searchLogoView = UIImageView.init(image: UIImage.init(named:"search_menu.png"))
        
        self.searchTextField.rightView = searchLogoView
        self.searchTextField.rightViewMode = .Always
        self.searchTextField.delegate = self
        self.searchTextField.rightViewRectForBounds(CGRect(x: 0, y: 0, width: searchTextField.bounds.height, height: searchTextField.bounds.height))
        
        
        let tapMaskViewOrToBarViewGesture = UITapGestureRecognizer.init(target: self, action: "respondsToTapGesture:")
        self.topBarView.addGestureRecognizer(tapMaskViewOrToBarViewGesture)
        
        let widthForRedLine = UIScreen.mainScreen().bounds.width/CGFloat(self.viewControllers.count)*0.6
        self.redLine.bounds = CGRect.init(x: 0, y: 0, width: widthForRedLine, height: widthForRedLine*0.1)
        self.tabBarView.addSubview(self.redLine)
    }
    
    //MARK: action / callback
    func tapedButton(sender:UIButton){
        self.selecedIndex = sender.tag - buttonTag
    }
    
    func respondsToTapGesture(gustrue:UITapGestureRecognizer){
        self.searchTextField.endEditing(true)
    }
    
    //MARK:UITextFieldDelegate
    func textFieldShouldEndEditing(textField: UITextField) -> Bool {
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.mask.alpha = 0;
            self.searchView.center = self.centerBeforeAnimation
            }) { (completion) -> Void in
                self.mask.removeFromSuperview()
                self.searchView.removeFromSuperview()
        }
        textField.resignFirstResponder()
        return true
    }
    
    var centerBeforeAnimation : CGPoint{
        return CGPoint.init(x: self.view.bounds.width/2, y: -self.searchView.bounds.height/2)
    }
    var centerAfterAnimation : CGPoint {
        print(CGPoint.init(x: self.view.bounds.width/2, y: self.tabBarView.bounds.height+self.searchView.bounds.height))
        return CGPoint.init(x: self.view.bounds.width/2, y: self.tabBarView.bounds.height+self.searchView.bounds.height/2)
    }
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        self.view.insertSubview(self.mask, atIndex: 1)
        self.view.insertSubview(self.searchView, atIndex: 2)
        
        self.searchView.center = centerBeforeAnimation
        UIView.animateWithDuration(0.5){
            self.mask.alpha = 1
            self.searchView.center = self.centerAfterAnimation
        }
        return true
    }






}
